package speedtax;

import java.util.Date;

import com.speedtax.tx.api.Address;
import com.speedtax.tx.api.CalculateInvoiceResult;
import com.speedtax.tx.api.CalculationError;
import com.speedtax.tx.api.FullAddress;
import com.speedtax.tx.api.Invoice;
import com.speedtax.tx.api.InvoiceTax;
import com.speedtax.tx.api.InvoiceType;
import com.speedtax.tx.api.LineItem;
import com.speedtax.tx.api.LineItemBundle;
import com.speedtax.tx.api.Price;
import com.speedtax.tx.api.ResolveAddressResult;
import com.speedtax.tx.api.ResolveAddressResultType;
import com.speedtax.tx.api.TaxJurisdiction;
import com.speedtax.tx.api.client.STxTransactionServiceClient;


public class TestCXF {
	
	private static final String TEST="http://service.demo.myspeedtax.com/tx/services/STxTransactionService";
	private static final String TEST_NEW="https://demo.myspeedtax.com/tx/services/STxTransactionService";
	private static final String LIVE_HTTP="http://service.p1.myspeedtax.com/tx/services/STxTransactionService";
	private static final String LIVE_HTTPS="https://service.myspeedtax.com/tx/services/STxTransactionService";
	
	private static final String url1=LIVE_HTTPS;
	private static final String url2="http://ec2-204-236-253-148.compute-1.amazonaws.com:8080/tx/services/STxTransactionService";
	private static final String url3="http://ec2-204-236-211-220.compute-1.amazonaws.com:8080/tx/services/STxTransactionService";

	String testUser="xx";
    String testPwd="xx";
    String testCompany="xx";
    
    String gfUser="xx";
    String gfPwd="xx";
    String gfCompany="xx";
	
	/**
	 * @param args
	 */
	public static void main(String[] args)throws Exception {
		TestCXF cxf=new TestCXF(); 
		//cxf.calculatePost();
		
		cxf.calculateInvoice();
		//cxf.ping();
		//cxf.resolveAddress();
		
		//cxf.springTest();
	}
	
	
	public void calculateInvoice() throws Exception{
	    
	    
		//STxTransactionServiceClient client = new STxTransactionServiceClient (TEST_NEW, testUser, testPwd);
		STxTransactionServiceClient client = new STxTransactionServiceClient (url1, gfUser, gfPwd);
		Invoice invoice = new Invoice();
        invoice.setInvoiceDate (new Date ());
        invoice.setInvoiceType (InvoiceType.INVOICE);
        invoice.setCustomerIdentifier("121");
        
        Address ak = new Address();
        ak.setAddress1("840 W FIREWEED LN");
        ak.setAddress2("ANCHORAGE, AK 99503-2508");
        
        Address cn = new Address();
        cn.setAddress1("first ring road");
        cn.setAddress2("cheng du, si chuan 610041");
        
        Address ca=new Address();
        ca.setAddress1("6 Journey Street");
        ca.setAddress2("ALISO VIEJO, CA 92659");
        
        Address fl=new Address();
        //fl.setAddress1("6 Journey Street");
        fl.setAddress2("Tampa, FL 33601");
        
        Address ma=new Address();
        ma.setAddress1("95 Walnut Street");
        ma.setAddress2("Lynnfield, MA 01940");
        
        Address ma2=new Address();
        ma2.setAddress1("46 Tally Ho Drive");
        ma2.setAddress2("South  Hamilton, MA 01982");
        
        Address gfFrom=new Address();
        gfFrom.setAddress1("9 State Street");
        gfFrom.setAddress2("Woburn, MA 01801");
        
        Address fl2=new Address();
        fl2.setAddress1("1442 Jackson Ave");
        fl2.setAddress2("CHIPLEY, FL 33601");
        
        Address to=new Address();
        to.setAddress1("15009 moorpark street #302");
        to.setAddress2("Sherman Oaks, CA 91403");
        
        Address to2=new Address();
        to2.setAddress1("2143 S Via Alonso");
        to2.setAddress2("Green Valley, MA 85614");
        
        Address to3=new Address();
        to3.setAddress1("25471 Hayes Avenue"); 
        to3.setAddress2("Murrieta, CA 92562");
        
        Address to38771=new Address();
        //error: 1673 Hartford Glen, N.E.
        to38771.setAddress1("1673 Hartford Glen N.E."); 
        to38771.setAddress2("Atlanta, GA 30319");
        
        double ip=17.49;
        double freight=0.0;
        invoice.addLineItem (gfFrom, ca, 1, new Price(24.95), "G-P1028", new Price(24.95));
        //invoice.addLineItem (gfFrom, to, 1, new Price(2.95), "GANO778", new Price(2.95));
        //invoice.addLineItem (gfFrom, to, 1, new Price(29.00), "C1000AAS", new Price(29.00));
        invoice.addLineItem (gfFrom, ca, 1, new Price(freight), "FREIGHT", new Price(freight));
        //invoice.setCustomerIdentifier("xx customerIdentifier xx");
        /*
        invoice.addLineItem (ca, ca, 1, new Price(5.2), "sku-3", new Price(5.2));
        invoice.addLineItem (ca, ca, 1, new Price(2.0), "FREIGHT", new Price(2.0));*/
        
        //invoice.addLineItem (ca, ca, 1, new Price(50.0), "sku-3", new Price(50.0));
        
        
        //invoice.addLineItem (cn, ak, 1, new Price(100.0), "sku-1", new Price(100.0));
        //invoice.addLineItem (cn, ak, 1, new Price(100.0), "sku-2", new Price(100.0));
        
        System.out.println(invoice);

        //CalculateInvoiceResult result = client.calculateInvoice(testCompany, invoice);
        CalculateInvoiceResult result = client.calculateInvoice(gfCompany, invoice);
        dumpResult (result);
        
        System.out.println(result);
	}
	
	public void calculatePost() throws Exception{
		STxTransactionServiceClient client = new STxTransactionServiceClient (url3, gfUser, gfPwd);
		Invoice invoice = new Invoice("post_invoice_789");
        invoice.setInvoiceDate (new Date ());
        invoice.setInvoiceType (InvoiceType.INVOICE);
        invoice.setCustomerIdentifier("121");
        
        Address ak = new Address();
        ak.setAddress1("840 W FIREWEED LN");
        ak.setAddress2("ANCHORAGE, AK 99503-2508");
        
        Address cn = new Address();
        cn.setAddress1("first ring road");
        cn.setAddress2("cheng du, si chuan 610041");
        
        Address ca=new Address();
        ca.setAddress1("6 Journey Street");
        ca.setAddress2("ALISO VIEJO, CA 92659");
        
        Address fl=new Address();
        //fl.setAddress1("6 Journey Street");
        fl.setAddress2("Tampa, FL 33601");
        
        Address ma=new Address();
        ma.setAddress1("19 Deerfield Street");
        ma.setAddress2("Boston, MA 02215");
        
        Address ma2=new Address();
        ma2.setAddress1("46 Tally Ho Drive");
        ma2.setAddress2("South  Hamilton, MA 01982");
        
        Address fl2=new Address();
        fl2.setAddress1("1442 Jackson Ave");
        fl2.setAddress2("CHIPLEY, FL 33601");
        
        invoice.addLineItem (ma2, ma2, 1, new Price(2.5), "sku-2", new Price(2.5));
        invoice.addLineItem (ma2, ma2, 1, new Price(), "FREIGHT", new Price());
        /*
        invoice.addLineItem (ca, ca, 1, new Price(5.2), "sku-3", new Price(5.2));
        invoice.addLineItem (ca, ca, 1, new Price(2.0), "FREIGHT", new Price(2.0));*/
        
        //invoice.addLineItem (ca, ca, 1, new Price(50.0), "sku-3", new Price(50.0));
        
        
        //invoice.addLineItem (cn, ak, 1, new Price(100.0), "sku-1", new Price(100.0));
        //invoice.addLineItem (cn, ak, 1, new Price(100.0), "sku-2", new Price(100.0));
        
        System.out.println(invoice);

        CalculateInvoiceResult result = client.postInvoice("STX-02155-00", invoice);
        dumpResult (result);
        
        //System.out.println(result);
	}
	
	public void ping() throws Exception{
		STxTransactionServiceClient client = new STxTransactionServiceClient (url3, gfUser, gfPwd);

        System.out.println(client.ping());
        
	}
	
	public void resolveAddress() throws Exception{
		STxTransactionServiceClient client = new STxTransactionServiceClient (url1, gfUser, gfPwd);
		Address from = new Address();
        from.setAddress1("840 W FIREWEED LN");
        from.setAddress2("ANCHORAGE, AK 99503-2508");
        
        Address from2 = new Address();
        //from2.setAddress1("6 Journey Street 2");
        from2.setAddress2("ALISO VIEJO, AK 92659");
        
        Address fl=new Address();
        fl.setAddress1("6 Journey Street");
        fl.setAddress2("Tampa, FL 33601");
        
        Address wrong=new Address();
        wrong.setAddress1("2143 S Via Alonso");
        wrong.setAddress2("Green Valley, MA 85614");
        
        Address wrong2=new Address();
        wrong2.setAddress1("2143 S Via Alonso");
        wrong2.setAddress2("Greenx Valleyx, MA 85614");
        
        System.out.println(wrong);
        
        ResolveAddressResult result =client.resolveAddress(wrong);
        System.out.println ("ResultType:"+result.getResultType ());
        if (!ResolveAddressResultType.UNRESOLVED.equals (result.getResultType ())) {
            System.out.println ("Address:"+result.getResolvedAddress ().getAddress ());
            System.out.println ("City:"+result.getResolvedAddress ().getCity ());
            System.out.println ("State:"+result.getResolvedAddress ().getState ());
            System.out.println ("Zip:"+result.getResolvedAddress ().getZip ());
            System.out.println("Tax Jurisdiction:");
            for (TaxJurisdiction tj : result.getJurisdictions ())
                System.out.println ("  " + tj.getJurisdictionFips () + " " + tj.getJurisdictionName ());
        }
        
	}
	
	private void dumpResult(CalculateInvoiceResult result) {
	    if (!result.getErrors ().isEmpty ()) {
	        System.out.println ("Errors:");
	        for (CalculationError error : result.getErrors ())
	            System.out.println ("  " + error.getType () + ": " + error.getMessage ());
	    } else {
	        System.out.println (result.getResultType ());
	        System.out.println ("TotalSales="+result.getTotalSales() + " TotalExemptSales=" + result.getTotalExemptSales() + " TotalTax=" + result.getTotalTax());
	        int i=0;
	        for (LineItemBundle bundle : result.getLineItemBundles ()) {
	            FullAddress address = bundle.getAddressForTax ();
	            System.out.println ("bundle "+i+"---"+address.getAddress () + " " + address.getCity () + ", " + address.getState () + " " + address.getZip ());
	            for (InvoiceTax tax : bundle.getTaxes ())
	                System.out.println ("  LineItemBundle tax: "+ tax.getJurisdictionFips ()+" "+tax.getJurisdictionLevel()+" "+ tax.getJurisdictionName ()+ " "+tax.getEffectiveRate() + "  " + tax.getTotalTax().toString());
	            
	            for (LineItem lineItem : bundle.getLineItems ()){
	                System.out.println ("    orig addr   [" +lineItem.getShipToAddress()+"] "+lineItem.getTaxAmount());
	                for(InvoiceTax tax: lineItem.getTaxes()){
	                	System.out.println ("    LineItem tax:  "+lineItem.getProductCode()+"[QTY "+lineItem.getQuantity()+"]"+tax.getJurisdictionFips ()+" "+tax.getJurisdictionLevel() + " " + tax.getJurisdictionName () + "  "+tax.getEffectiveRate()+" " + tax.getTotalTax().toString());
	                }
	            }
	            
	            i++;
	        }
	    }
	}

}
